//
//  WSManager+Bookings.m
//  mUBB
//
//  Created by Cosmin Stirbu on 2/20/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "WSManager+Bookings.h"
#import "BookingsHandler.h"
#import "Utils.h"
#import "DeleteBookingHandler.h"
#import "UpdateBookingHandler.h"
#import "ConfigurationManager.h"
#import "AddBookingHandler.h"

#define DATE_FORMAT @"yyyy-MM-dd"
#define DATE_TIME_FORMAT @"yyyy-MM-dd HH:mm:ss"
#define DATE_PARAMETER @"{date}"

@implementation WSManager (Bookings)

- (Ticket *)getBookingsForCourse:(Course *)course delegate:(id<BookingsProtocol>)delegate {
    // New ticket
    Ticket *ticket = [[Ticket alloc] initWithId:ticketId++];
    
    // Connection delegate
    connectionDelegate = [[BookingsHandler alloc] initWithDelegate:delegate ticket:ticket];
    
    // Make the request
    [self getRequestWithUrl:course.bookingsUrl andHeaders:nil];
    
    // Set Connection Delegate to nil
    connectionDelegate = nil;
    
    return ticket;
}

- (Ticket *)getBookingsForRoom:(Room *)room withDate:(NSDate *)date delegate:(id<BookingsProtocol>)delegate {
    // New ticket
    Ticket *ticket = [[Ticket alloc] initWithId:ticketId++];
    // Connection delegate
    connectionDelegate = [[BookingsHandler alloc] initWithDelegate:delegate ticket:ticket];

    // Date as string
    NSString *dateAsString = [Utils stringFromDate:date withFormat:DATE_FORMAT];
    
    // URL
    NSString *url = [room.bookingsURL stringByReplacingOccurrencesOfString:DATE_PARAMETER withString:dateAsString];
    
    // Make the request
    [self getRequestWithUrl:url andHeaders:nil];
    
    // Set Connection Delegate to Nil
    connectionDelegate = nil;
    
    return ticket;
}

- (Ticket *)createBooking:(Booking *)booking delegate:(id<BookingsProtocol>)delegate {
    NSString *url = [UserDefaultsManager getObjectForKey:WS_BOOKINGS_SERVICE_KEY];
    // New ticket
    Ticket *ticket = [[Ticket alloc] initWithId:ticketId++];
    // Connection delegate
    connectionDelegate = [[AddBookingHandler alloc] initWithDelegate:delegate ticket:ticket];
    
    // Make the request
    [self postRequestWithUrl:url body:[self booking:booking toJSONForAdding:YES] andHeaders:nil];
    
    // Set Connection Delegate to Nil
    connectionDelegate = nil;
    
    return ticket;
}

- (Ticket *)updateBooking:(Booking *)booking delegate:(id<BookingsProtocol>)delegate {
    // New ticket
    Ticket *ticket = [[Ticket alloc] initWithId:ticketId++];
    // Connection delegate
    connectionDelegate = [[UpdateBookingHandler alloc] initWithDelegate:delegate ticket:ticket];
    
    // Make the request
    [self putRequestWithUrl:booking.selfURL body:[self booking:booking toJSONForAdding:NO] andHeaders:nil];
    
    // Set Connection Delegate to Nil
    connectionDelegate = nil;
    
    return ticket;
}

- (Ticket *)deleteBooking:(Booking *)booking delegate:(id<BookingsProtocol>)delegate {
    // New ticket
    Ticket *ticket = [[Ticket alloc] initWithId:ticketId++];
    // Connection delegate
    connectionDelegate = [[DeleteBookingHandler alloc] initWithDelegate:delegate ticket:ticket];
    
    // Make the request
    [self deleteRequestWithUrl:booking.selfURL body:nil andHeaders:nil];
    
    // Set Connection Delegate to Nil
    connectionDelegate = nil;
    
    return ticket;
}


- (NSString *)booking:(Booking *)booking toJSONForAdding:(BOOL)forAdding {
    NSMutableDictionary *bookingDictionary = [NSMutableDictionary new];
    
    [bookingDictionary setObject:[Utils stringFromDate:booking.startDate withFormat:DATE_TIME_FORMAT] forKey:BOOKING_START_DATE_TAG];
    [bookingDictionary setObject:[Utils stringFromDate:booking.endDate withFormat:DATE_TIME_FORMAT] forKey:BOOKING_END_DATE_TAG];
    if (forAdding) {
        [bookingDictionary setObject:booking.course.courseCode forKey:COURSE_CODE_TAG];
        [bookingDictionary setObject:[[ConfigurationManager instance] applicationConfigValueForKey:USER_ID] forKey:TEACHER_ID_TAG];
    }
    [bookingDictionary setObject:[NSNumber numberWithInt:booking.room.roomId] forKey:ROOM_ID_TAG];
    [bookingDictionary setObject:[NSNumber numberWithInt:booking.typeOfClass.typeOfClassId] forKey:TYPE_OF_CLASS_ID_TAG];
    NSMutableArray *studyGroups = [NSMutableArray new];
    for (StudyGroup *studyGroup in booking.studyGroups) {
        NSMutableDictionary *studyGroupDict = [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithInt:studyGroup.studyGroupId] forKey:STUDY_GROUP_ID_TAG];
        [studyGroups addObject:studyGroupDict];
    }
    [bookingDictionary setObject:studyGroups forKey:BOOKING_STUDY_GROUPS_TAG];
    return [bookingDictionary JSONString];
}

@end
